/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.webmev.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.Callable;

/**
 *
 * @author Victor
 */
public class CountInputStream extends InputStream {

    public interface Action {
        public void apply (int c);
    }

    private final InputStream in;
    private long count = 0;
    private final Action action;

    public CountInputStream (InputStream in, Action action) {
        this.in = in;
        this.action = action;
    }

    public CountInputStream (InputStream in) {
        this (in, new Action () {
            public void apply (int c) {}
        });
    }

    @Override
    public int read() throws IOException {
        int c = in.read ();
        if (c != -1) {
            action.apply (c);
            count++;
        }
        return c;
    }

    public long count () { return count; }

    public void resetCount () { count = 0; }
}
